package com.java.fileman;

import java.io.*;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class AccessObject {

    public static String FILE = ".ftaccess";
    public JSONObject jsonObject;

    public AccessObject(String path) throws FileNotFoundException, IOException {
        JSONParser parser = new JSONParser();
        try {

            Object obj = parser.parse(new FileReader(path));
            jsonObject = (JSONObject) obj;

        } catch (org.json.simple.parser.ParseException ex) {
            Logger.getLogger(AccessObject.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public JSONObject getHson(){
        return jsonObject;
    }
    public boolean checckAccess(String group, String file) {
        boolean flag = false;
        if (jsonObject.containsKey(group)) {
            JSONObject permission = (JSONObject) jsonObject.get(group);
            
            // checking for allow access
            Object allow = permission.get("allow");
            if (allow instanceof String) {
                if(allow.equals("*")){
                    flag = true;
                }
            }else if(allow instanceof JSONObject){
                JSONArray msg = (JSONArray) jsonObject.get("allow");
                Iterator<String> iterator = msg.iterator();
                while (iterator.hasNext()) {
                    String entry = iterator.next();
                    if(entry.equals(file)){
                        flag = true;
                    }
		}
            }

            // checking for deny access
            Object deny = permission.get("deny");
            if (deny instanceof String) {
                if(deny.equals("*")){
                    flag = false;
                }
            }else if(deny instanceof JSONObject){
                JSONArray msg = (JSONArray) jsonObject.get("deny");
                Iterator<String> iterator = msg.iterator();
                while (iterator.hasNext()) {
                    String entry = iterator.next();
                    if(entry.equals(file)){
                        flag = false;
                    }
		}
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        try {
            AccessObject ao = new AccessObject("D:\\abhishek\\downloads\\.ftaccess");
            System.out.println(ao.getHson());
            System.out.println(ao.checckAccess("admin", "test"));
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AccessObject.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AccessObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String toString(){
        return jsonObject.toJSONString();
    }
}
